/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 */

import { JsonReader } from '../../../util/json-reader.js';
import { BasicFileSnapshot } from '../core/filesystem-snapshot.js';
import { FileSystemSnapshotGenerator } from '../generator/filesystem-snapshot-generator.js';
import { FsOptions } from '../util/file-set.js';

export class DefaultFsSnapshotGenerator implements FileSystemSnapshotGenerator {
  /**
   * Snapshot a file or directory
   *
   * @param location location
   * @param options
   */
  snapshot(location: string, options?: FsOptions): BasicFileSnapshot {
    const result: any = 0;
    return result;
  }

  loadSnapshotCacheFromJson(jsonStr: string): Map<string, BasicFileSnapshot> | undefined {
    const obj = JsonReader.parseJsonText<Map<string, BasicFileSnapshot>>(jsonStr, new Map<string, BasicFileSnapshot>());
    const map = new Map<string, BasicFileSnapshot>();
    for (const [key, value] of Object.entries(obj)) {
      map.set(key, Object.setPrototypeOf(value, BasicFileSnapshot.prototype));
    }
    return map;
  }

  loadSnapshotFromJson(jsonStr: string): BasicFileSnapshot | undefined {
    const snapshot = JsonReader.parseJsonText<BasicFileSnapshot>(jsonStr, {} as BasicFileSnapshot);
    Object.setPrototypeOf(snapshot, BasicFileSnapshot);
    return snapshot;
  }

  serializeSnapshotToJson(map: Map<string, BasicFileSnapshot>): string {
    const obj = Object.fromEntries(map.entries());
    return JSON.stringify(obj);
  }
}
